package controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;

import dal.AuthenticationDAO;
import dto.AuthenticationDTO;

public class AuthenticationController {
	private String _username;
	private String _password;
	private AuthenticationDAO _dao;

	public AuthenticationController() {
		this._dao = new AuthenticationDAO();
	}

	public AuthenticationController(String _username, String _password) {
		this._username = _username;
		this._password = _password;
		this._dao = new AuthenticationDAO();
	}

	public String get_username() {
		return _username;
	}

	public void set_username(String _username) {
		this._username = _username;
	}

	public String get_password() {
		return _password;
	}

	public void set_password(String _password) {
		this._password = _password;
	}

	/**
	 * Get all accounts
	 * 
	 * @return
	 * @throws JsonIOException
	 * @throws JsonSyntaxException
	 * @throws FileNotFoundException
	 */
	public ArrayList<AuthenticationDTO> getAllAccounts()
			throws JsonIOException, JsonSyntaxException, FileNotFoundException {
		return _dao.getAllAccount();
	}

	/**
	 * Check login info
	 * 
	 * @return
	 * @throws Exception
	 */
	public boolean isValidAuthenticationInfo() throws Exception {
		AuthenticationDTO info = _dao.getAccount(_username);
		if (info != null) {
			// account found.
			if (info.get_password().equals(this._password)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Create an account.
	 * 
	 * @return
	 * @throws IOException
	 */
	public boolean createAccount() throws IOException {
		return _dao.createAccount(new AuthenticationDTO(_username, _password));
	}

	/**
	 * Remove account
	 * 
	 * @return
	 * @throws IOException
	 */
	public boolean removeAccount() throws IOException {
		return _dao.removeAccount(new AuthenticationDTO(_username, _password));
	}
}
